package score;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import lecture.Lecture;
import lecture.LectureManagedBean;
import task.ExerciseSheet;
import task.TaskCategory;
import user.User;
import task.Task;

/**
 *
 * @author Thorsten Ludwig
 */
@ManagedBean(name="viewAllScoreBean")
@RequestScoped
public class ViewAllScoreManagedBean {
    
    private List<UserTasksContainer> list;
    
    private List<String> captions;
    

    public ViewAllScoreManagedBean() {
        this.init();
        this.initCaption();
    }

    public List<UserTasksContainer> getList() {
        return list;
    }

    public void setList(List<UserTasksContainer> list) {
        this.list = list;
    }
    
    private void init() {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        Map<ExerciseSheet, ScoreData> scores = lecture.getScore();
        List<UserTasksContainer> userTasks = new ArrayList<UserTasksContainer>();
        
        for (User u: lecture.getStudents()) {
            System.out.println("Student: " + u.getUsername());
            List<SheetContainer> tasks = new ArrayList<SheetContainer>();
            for (TaskCategory tc: lecture.getExerciseCategories()) {
                System.out.println("TaskCategory: " + tc.getName());
                for (ExerciseSheet es: this.getExerciseSheetByTaskCategory(tc, lecture)) {
                    System.out.println("Sheet: " + es.getName());
                    List<Score> s = scores.get(es).getField().get(u.getUsername());
                    String sheetName = es.getName();
                    double sum = 0;
                    double percent = 0;
                    boolean req = false;
                    boolean valued = false;
                    if (this.isValued(s)) {
                       valued=true;
                       sum = this.getSum(s);
                       double max = this.getMaxScore(es.getExercises(),s);
                       if (max != 0)
                            percent = (sum / max) * 100;
                       if (percent >= tc.getRequirement())
                           req = true; 
                    } else {
                        req = true;
                    }
                    
                    tasks.add(new SheetContainer(req,sheetName,sum,percent, valued));
                }
                String name = "Gesamtpunktzahl " + tc.getName();
                double percent = this.getTcPercent(tasks);
                double sum = this.getTcSum(tasks);
                boolean req = (percent >= tc.getRequirement() || !this.isValuedAll(tasks));
                tasks.add(new SheetContainer(req,name,sum,percent,true));
            }
            
            userTasks.add(new UserTasksContainer(u, tasks));
        }
        this.list = userTasks;
    }
    
    public boolean isValued(List<Score> scores) {
        for (Score s: scores) {
            if (s.getStatus() != Status.EXCUSED) {
                return true;
            }
        }
        return false;
    }
    private List<ExerciseSheet> getExerciseSheetByTaskCategory(TaskCategory tc, Lecture l) {
        List<ExerciseSheet> sheets = new ArrayList<ExerciseSheet>();
        for (ExerciseSheet es: l.getExerciseSheets()) {
            if (es.getCategory().equals(tc))
                sheets.add(es);
        }
        return sheets;
        
    }

    private double getSum(List<Score> scores) {
        double returnVal = 0;
        for (Score s: scores) {
            returnVal += s.getScore();
        }
        return returnVal;
    }

    private double getMaxScore(List<Task> tasks, List<Score> scores) {
        double returnVal = 0;
        for (int i = 0; i < tasks.size(); i++) {
            if (scores.get(i).getStatus() != Status.EXCUSED)
                returnVal += tasks.get(i).getMaxScore();
        }
        return returnVal;
    }

    private double getTcPercent(List<SheetContainer> tasks) {
        double percent = 0;
        int counter = 0;
        for (SheetContainer sc: tasks) {
            if (sc.isValued()) {
                counter++;
                percent += sc.getPercent();
            }
        }
        return percent;
    }

    private double getTcSum(List<SheetContainer> tasks) {
        double sum = 0;
        for (SheetContainer sc: tasks) {
            sum += sc.getSum();
        }
        return sum;
    }

    private void initCaption() {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        
        List<String> listCaptions = new ArrayList<String>();
        for (TaskCategory tc: lecture.getExerciseCategories()) {
            for (ExerciseSheet es: this.getExerciseSheetByTaskCategory(tc, lecture)) {
                listCaptions.add(es.getName());
            }
            listCaptions.add("Gesamtpunktzahl " + tc.getName());
        }
        this.captions = listCaptions;
        
        
    }

    public List<String> getCaptions() {
        return captions;
    }

    private boolean isValuedAll(List<SheetContainer> tasks) {
        for(SheetContainer sc: tasks) {
            if (sc.isValued())
                return true;
        }
        return false;
    }
    
    
}
